"""
@Author    : ghenyar
@Time      : 2025/9/5 13:05
@File      : admin
@Desc      : 
"""
from typing import Optional, List,Union

from pydantic import BaseModel, ConfigDict, field_validator

from app.common.exception.exceptions import ValidationException
from app.common.tools import format_datetime

SEX_MAP = {1: "男", 2: "女"}

class AdminSchema(BaseModel):
    """管理员表单校验"""
    id: Optional[int] = None
    account: str
    password: Optional[str] = None
    nickname: str
    phone: Optional[str] = None
    email: Optional[str] = None
    avatar: Optional[int] = None
    is_locked: Optional[int] = None
    role: int

    @field_validator("account")
    def validate_account(cls, v):
        if not v:
            raise ValidationException("账号不能为空")
        return v

    @field_validator("password")
    def validate_password(cls, v, info):
        admin_id = info.data.get("id")
        if (admin_id == 0 or admin_id is None) and not v:
            raise ValidationException("登录密码不能为空")
        return v

    @field_validator("nickname")
    def validate_nickname(cls, v):
        if not v:
            raise ValidationException("昵称不能为空")
        return v

    @field_validator("role")
    def validate_role(cls, v):
        if not v:
            raise ValidationException("角色不能为空")
        return v

    model_config = ConfigDict(from_attributes=True)

class AdminSerialize(BaseModel):
    id: int
    account: str
    nickname: str
    phone: Optional[str] = None
    email: Optional[str] = None
    is_disabled: Optional[bool] = False
    is_super: Optional[bool] = False
    role_name: Optional[List[str]] = None
    role: Optional[int] = None
    created_at: str
    updated_at: Optional[str] = None
    avatar: Optional[str] = None
    is_locked:int

    @classmethod
    async def from_orm(cls, obj):

        return cls(
            id=obj.id,
            account=obj.account,
            nickname=obj.nickname,
            phone=obj.phone,
            email=obj.email,
            avatar=getattr(obj, 'avatar', None),
            is_locked=getattr(obj, 'is_locked', 0),
            created_at=format_datetime(obj.created_at),
            updated_at=format_datetime(obj.updated_at) if obj.updated_at else None,
            is_disabled=bool(getattr(obj, 'is_super', False)),
            is_super=bool(getattr(obj, 'is_super', False)),
            role=getattr(obj, 'role', None),
            role_name=getattr(obj, 'role_name', None)
        )

    model_config = ConfigDict(from_attributes=True)


class AdminUserInfo(BaseModel):
    account: str
    nickname: str
    phone: Optional[str] = None
    email: Optional[str] = None
    role_name: Union[Optional[List[str]],str] = None
    avatar: Optional[str] = None

    @classmethod
    async def from_orm(cls, obj):
        return cls(
            account=obj.account,
            nickname=obj.nickname,
            phone=obj.phone,
            email=obj.email,
            avatar=getattr(obj, 'avatar', None),
            role_name=getattr(obj, 'role_name', None)
        )

    model_config = ConfigDict(from_attributes=True)

class AdminLogSerialize(BaseModel):
    id: int
    admin_id: int
    account: Optional[str] = None
    method: Optional[str] = None
    uri: Optional[str] = None
    req_param: Optional[str] = None
    result: Optional[str] = None
    result_msg: Optional[str] = None
    status_code: int
    ip: Optional[str] = None
    web_browser: Optional[str] = None
    os_name: Optional[str] = None
    desc_message: Optional[str] = None
    type: int
    created_at: str

    @classmethod
    async def from_orm(cls, obj):
        return cls(
            id=obj.id,
            admin_id=obj.admin_id,
            account=obj.account,
            method=obj.method,
            uri=obj.uri,
            req_param=obj.req_param,
            result=obj.result,
            result_msg=obj.result_msg,
            status_code=obj.status_code,
            ip=obj.ip,
            web_browser=obj.web_browser,
            os_name=obj.os_name,
            desc_message=obj.desc_message,
            type=obj.types,
            created_at=format_datetime(obj.created_at),
        )

    model_config = ConfigDict(from_attributes=True)


class UpdateLoginAminSchema(BaseModel):
    """更新当前登录账号个人信息"""
    nickname: str
    phone: Optional[str] = None
    email: Optional[str] = None
    avatar: Optional[str] = None

    @field_validator("nickname")
    def validate_nickname(cls, v):
        if not v:
            raise ValueError("昵称不能为空")
        return v

    model_config = ConfigDict(from_attributes=True)